package com.huabo.audit.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.audit.oracle.entity.TblNbsjBorrowRecordEntity;
import com.huabo.audit.oracle.vo.TblnbsjProjectVo;
import com.huabo.audit.service.TblNbsjBorrowRecordService;
import com.huabo.audit.service.TblNbsjProjectService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 审计档案管理
 */
@RestController
@Slf4j
@Api(value = "审计档案管理", tags = { "审计档案管理接口" })
@RequestMapping(value = "/auditArchive")
public class NbsjArchiveController {
	
	@Resource
	public TblNbsjProjectService tblnbsjProjectService;
	
	@Resource
	public TblNbsjBorrowRecordService tblNbsjBorrowRecordService;
	
	
	
	/**
	 * 档案列表
	 */
	@GetMapping("/sjgd/sjgd_newlist")
	@ApiOperation(value = "档案列表")
	public JsonBean sjgd_newlist(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,TblnbsjProjectVo tblnbsjProjectVo,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {

		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblnbsjProjectService.sjgdNewPageList(token, pageNumber, pageSize, tblnbsjProjectVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	/**
	 * 档案列表-明细
	 */
	@GetMapping("/sjgd/sjgd_new_detail")
    @ApiOperation(value = "档案列表-明细")
    public JsonBean sjgd_new_detail(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="projectid",value="主键",required=true)@RequestParam(value = "projectid", required = true) Integer projectid) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblnbsjProjectService.findSjgdNewDetail(token,projectid);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
	
	/**
	 * 档案借阅
	 */
	@GetMapping("/sjgd/dajy_newlist")
	@ApiOperation(value = "档案借阅列表")
	public JsonBean dajy_newlist(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {

		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblnbsjProjectService.dajyNewPageList(token, pageNumber, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	/**
	 * 档案借阅-申请借阅
	 */
	@RequestMapping(value = "/jhgl/tjspBorrow", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "档案借阅-申请借阅")
    public JsonBean tjspBorrow(HttpServletRequest request,@ApiParam(name="br",value="实体",required=true)TblNbsjBorrowRecordEntity br,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
			 @ApiParam(name="borrowDate",value="借阅日期 格式年-月-日",required=false)@RequestParam(value = "borrowDate", required = false) String borrowDate,
		   		@ApiParam(name="backDate",value="归还日期 格式年-月-日",required=false)@RequestParam(value = "backDate", required = false) String backDate)throws Exception{
		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblNbsjBorrowRecordService.tjspBorrow(br,token,borrowDate,backDate);
		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
	
	/**
	 * 借阅日志列表
	 */
	@GetMapping("/sjgd/jyrz_newlist")
	@ApiOperation(value = "借阅日志列表")
	public JsonBean jyrz_newlist(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {

		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblnbsjProjectService.jyrzNewPageList(token, pageNumber, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	/**
	 * 借阅日志-借阅次数列表
	 */
	@GetMapping("/sjgd/jyrz_countlist")
	@ApiOperation(value = "借阅日志-借阅次数列表")
	public JsonBean jyrz_countlist(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "projectid", value = "projectid", required = false) @RequestParam("projectid") Integer projectid,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {

		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblNbsjBorrowRecordService.jyrzCountPageList(token, pageNumber, pageSize,projectid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	
	
	
	
	
	/*
	*//**
     * 审计档案管理--档案列表
     *
     *//*
    @RequestMapping(value = "/sjgd/sjgd_newlist")
    public ModelAndView sjgdlist(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        TblStaff user = (TblStaff) request.getSession().getAttribute("longUser");
        String pageNumber = request.getParameter("pageNumber");
        String projectname = request.getParameter("projectname");
        if (pageNumber == null || pageNumber == "") {
            pageNumber = "1";
        }
        String orgid = request.getParameter("orgid");
        mv.addObject("projectname", projectname);
        pageBean = new PageBean();
        //审计责任人、审计负责人可以看所有归档的项目
        if(user.getTblRole()!=null&&(user.getTblRole().getRname().equals("审计部负责人")||user.getTblRole().getRname().equals("审计责任人"))) {
        	pageBean = this.tblnbsjProjectService.findByProjectListByplanid(projectname, Integer.parseInt(pageNumber), pageBean.getPageSize());
        }else {
        	//项目经理看自己的项目
        	//工作人员看借阅的项目
        	pageBean = this.tblnbsjProjectService.findByProjectListByStaffid(projectname,user.getStaffid(),Integer.parseInt(pageNumber), pageBean.getPageSize());
        }
        mv.addObject("pageBean", pageBean);
        mv.addObject("orgid", orgid);
      //查询框代码
        String choiceSearch = request.getParameter("choiceSearch");
        if(choiceSearch == null || "".equals(choiceSearch)) {
        	choiceSearch = "hide";
        }
        mv.addObject("choiceSearch",choiceSearch); 
        mv.setViewName("nbsj/sjgl/sjgd_newlist");
        return mv;
    }
    *//**
     * 审计档案管理--档案借阅
     *//*
    @RequestMapping(value = "/sjgd/dajy_newlist")
    public ModelAndView dajy_newlist(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();
    	TblStaff user = (TblStaff) request.getSession().getAttribute("longUser");
    	String pageNumber = request.getParameter("pageNumber");
    	String projectname = request.getParameter("projectname");
    	if (pageNumber == null || pageNumber == "") {
    		pageNumber = "1";
    	}
    	String orgid = request.getParameter("orgid");
    	mv.addObject("projectname", projectname);
    	pageBean = this.tblnbsjProjectService.findByProjectListByUserid(projectname, Integer.parseInt(pageNumber), pageBean.getPageSize());
    	mv.addObject("pageBean", pageBean);
    	mv.addObject("orgid", orgid);
    	//查询框代码
    	String choiceSearch = request.getParameter("choiceSearch");
    	if(choiceSearch == null || "".equals(choiceSearch)) {
    		choiceSearch = "hide";
    	}
    	mv.addObject("choiceSearch",choiceSearch); 
    	mv.setViewName("nbsj/sjgl/dajy_newlist");
    	return mv;
    }
    *//**
  	 *  档案借阅--填写借阅信息
     * @return
     *//*
    @RequestMapping(value = "/sjgd/toBorrow", produces = "application/json; charset=utf-8")
    public ModelAndView contract_addLeadInfo(Model model,HttpServletResponse response,HttpServletRequest request)throws Exception{
    	ModelAndView mv = new ModelAndView("/nbsj/sjgl/toBorrow");
    	String projectid = request.getParameter("projectid");
    	TblNbsjProjectEntity project = tblnbsjProjectService.getById(projectid);
    	Date currentDate = new Date();
    	model.addAttribute("project", project);
    	model.addAttribute("creattime", currentDate);
		return mv;
    }
    
    
    *//**
     * @throws java.text.ParseException 
     * @Des:审计档案管理-借阅--提交审批
     *//*
    @RequestMapping(value = "/jhgl/tjspBorrow")
    public @ResponseBody String tjspBorrow(HttpServletRequest request) throws ParseException, java.text.ParseException {
        TblStaff user = (TblStaff) request.getSession().getAttribute("longUser");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        String projectid = request.getParameter("projectid");
        String returnDate = request.getParameter("returnDate");
        String memo = request.getParameter("memo");
        TblNbsjProjectEntity project = tblnbsjProjectService.getById(projectid);
        TblNbsjBorrowRecordEntity tblNbsjBorrowRecord = new TblNbsjBorrowRecordEntity(null, project, user, new Date(),simpleDateFormat.parse(returnDate),TblNbsjBorrowRecordEntity.SPKA,memo);
        tblNbsjBorrowRecordService.save(tblNbsjBorrowRecord);
        String str = "o";
        try {
           TblCirculation cir = tblCirculationService .saveTblCirculation(TblNbsjBorrowRecordEntity.TYPE_DAJY,project.getProjectCode(),project.getPProjectName()+"借阅",TblNbsjBorrowRecordEntity.URL_JYSP+tblNbsjBorrowRecord.getBorrowid(),user.getStaffid());
            try {
                processService.start(ProcessEnum.SJ_JHGL.name(),user,cir);
            } catch (Exception e) {
                str = "n";
                tblCirculationService.delTblCirculation(cir);
                e.printStackTrace();
            }
        } catch (Exception e) {
            str = "n";
            e.printStackTrace();
        }	
        return str;
    }
    *//**
     * @Author: TYB
     * @Date: 2016-12-02 下午 3:13
     * @Des:审批计划详情页
     *//*
    @RequestMapping(value = "/sjgd/toBorrowinfo")
    public ModelAndView toBorrowinfo(HttpServletRequest request,String borrowid) {
        TblStaff staff = (TblStaff) request.getSession().getAttribute("longUser");
        ModelAndView mv = new ModelAndView();
        mv.addObject("t",request.getParameter("t"));
        TblNbsjBorrowRecordEntity tblNbsjBorrowRecord = tblNbsjBorrowRecordService.getById(new BigDecimal(borrowid));
        TblNbsjProjectEntity project = tblnbsjProjectService.getById(tblNbsjBorrowRecord.getTblproject().getProjectId().toString());
    	if(tblNbsjBorrowRecord!=null){
    		
    		String tId = request.getParameter("tId");
    		if(StringUtils.isNotBlank(tId) && tblNbsjBorrowRecord.getStatus()!= 0){
    			List<String> btnList = processService.getButtonsForTransition(tId);
    			mv.addObject("btnList",btnList);
    		}
    		mv.addObject("v",request.getParameter("v"));
    		mv.addObject("tId",tId);
    		String cyId = request.getParameter("cyId");
    		List<TblAuditOption> ao = tblAuditOptionService.findOptionByRelation(borrowid,Integer.parseInt(cyId));
    		if (StringUtils.isNotBlank(cyId)){
    			TblCirculation cy = tblCirculationService.get(cyId);
    			mv.addObject("cy",cy);
    			mv.addObject("borrow",tblNbsjBorrowRecord);
    			mv.addObject("project",project);
    			mv.addObject("staff",staff);
    		}
    		mv.addObject("ao",ao);
    		//修改
    		mv.setViewName("nbsj/sjgl/toBorrowSpInfo");
    	}
    	return mv;
    }
    @RequestMapping(value = "/toBorrowinfoUpdate", produces = "application/json; charset=utf-8")
    public @ResponseBody String toBorrowinfoUpdate(HttpServletRequest request,String borrowid){
        String cyId = request.getParameter("cyId");
        String memo = request.getParameter("memo");
        TblNbsjBorrowRecordEntity tblNbsjBorrowRecord = tblNbsjBorrowRecordService.getById(new BigDecimal(borrowid));
        TblNbsjProjectEntity project = tblNbsjBorrowRecord.getTblproject();
        try {
        	tblNbsjBorrowRecord.setStatus(TblNbsjBorrowRecordEntity.SPKA);
        	tblNbsjBorrowRecord.setMemo(memo);
        	tblNbsjBorrowRecordService.saveOrUpdate(tblNbsjBorrowRecord);
            TblCirculation c = tblCirculationService.get(cyId);
            c.setCyname(project.getPProjectName());
            c.setCystate(TblCirculation.STATE_FQ);
            tblCirculationService.update(c);
        } catch (Exception e) {
            JsonBean.error("修改审批失败");
            e.printStackTrace();
        }
        return JsonBean.success();
    }
    *//**
     * 审计档案管理--借阅日志
     *
     * @param request
     * @param planid
     * @return
     *//*
    @RequestMapping(value = "/sjgd/jyrz_newlist")
    public ModelAndView jyrz_newlist(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();
    	TblStaff user = (TblStaff) request.getSession().getAttribute("longUser");
    	String pageNumber = request.getParameter("pageNumber");
    	String projectname = request.getParameter("projectname");
    	if (pageNumber == null || pageNumber == "") {
    		pageNumber = "1";
    	}
    	String orgid = request.getParameter("orgid");
    	mv.addObject("projectname", projectname);
    	pageBean = this.tblnbsjProjectService.findByProjectListByJYRZ(Integer.parseInt(pageNumber), pageBean.getPageSize());
    	mv.addObject("pageBean", pageBean);
    	mv.addObject("orgid", orgid);
    	//查询框代码
    	String choiceSearch = request.getParameter("choiceSearch");
    	if(choiceSearch == null || "".equals(choiceSearch)) {
    		choiceSearch = "hide";
    	}
    	mv.addObject("choiceSearch",choiceSearch); 
    	mv.setViewName("nbsj/sjgl/jyrz_newlist");
    	return mv;
    }
    *//**
     * 审计档案管理--借阅次数
     *
     * @param request
     * @param planid
     * @return
     *//*
    @RequestMapping(value = "/sjgd/jyrz_countlist")
    public ModelAndView jyrz_countlist(HttpServletRequest request) {
    	ModelAndView mv = new ModelAndView();
    	String pageNumber = request.getParameter("pageNumber");
    	if (pageNumber == null || pageNumber == "") {
    		pageNumber = "1";
    	}
    	String projectid = request.getParameter("projectid");
    	pageBean = this.tblNbsjBorrowRecordService.findBorrowCountByprojectid(projectid,Integer.parseInt(pageNumber), pageBean.getPageSize());
    	mv.addObject("pageBean", pageBean);
    	//查询框代码
    	String choiceSearch = request.getParameter("choiceSearch");
    	if(choiceSearch == null || "".equals(choiceSearch)) {
    		choiceSearch = "hide";
    	}
    	mv.addObject("choiceSearch",choiceSearch); 
    	mv.setViewName("nbsj/sjgl/jyrz_countlist");
    	return mv;
    }*/
}
